package com.example.gzq.mymusictest;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.gzq.mymusictest.domain.Person;

import org.litepal.crud.DataSupport;

import java.util.List;


public class LoginActivity extends BaseStyle {
    private static final String TAG = "LoginActivity";
    String mPhone="",mPsW="";
    boolean bool_btn=true;
    EditText mAccNum, mPassWord;
    ImageView mNumImg,mPswImg;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        mAccNum=(EditText)findViewById(R.id.mAccNum);
        mPassWord=(EditText)findViewById(R.id.mPassWord);
        mNumImg=(ImageView)findViewById(R.id.num_img);
        mPswImg=(ImageView)findViewById(R.id.loginPass_img);
        Button butLogin=(Button)findViewById(R.id.mLogin);
        Button butRegst=(Button)findViewById(R.id.mRegistered);
        loginCheck(mAccNum,"phone");//手机格式验证
        loginCheck(mPassWord,"password");//密码格式验证
        butLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mPhone=mAccNum.getText().toString();
                mPsW=mPassWord.getText().toString();
                if (checkedUser(mPhone,mPsW)&&bool_btn){
                    //将数据返回到上一个活动
                    Intent intent=new Intent();
                    intent.putExtra("mNum",mPhone);
                    Log.d(TAG, "onClick: mNum="+mPhone+"  mPaW="+mPsW);
                    setResult(RESULT_OK,intent);
                    finish();
                }else if (!bool_btn){
                    Toast.makeText(LoginActivity.this, "请输入正确的登录信息", Toast.LENGTH_SHORT).show();
                }else {
                    Toast.makeText(LoginActivity.this, "用户不存在", Toast.LENGTH_SHORT).show();
                }
            }
        });
        butRegst.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(LoginActivity.this,Registered.class);
                startActivity(intent);
                finish();
            }
        });
    }
    //验证用户登录信息是否正确
    protected boolean checkedUser(String phone,String password){
        boolean bool=false;
        List<Person> persons= DataSupport.findAll(Person.class);
        Log.d(TAG, "checkedUser: size="+persons.size());
        for (Person person:persons){
            if (person.getmTel().equals(phone)&&person.getmPassWord().equals(password)){
                bool=true;
            }
        }
        return bool;
    }
    //输入信息格式判断
    public void loginCheck(EditText editText, final String s){
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }
            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }
            @Override
            public void afterTextChanged(Editable editable) {
                if (s.equals("phone")){
                    if (isId(editable.toString())) {
                        mNumImg.setImageResource(R.drawable.success);
                        bool_btn=true;
                    }else {
                        mNumImg.setImageResource(R.drawable.wrong);
                        bool_btn=false;
                    }
                }else if (s.equals("password")){
                    if (isPsw(editable.toString())) {
                        mPswImg.setImageResource(R.drawable.success);
                        bool_btn=true;
                    }else {
                        mPswImg.setImageResource(R.drawable.wrong);
                        bool_btn=false;
                    }
                }
            }
        });
    }
    //手机号格式判断
    public static boolean isId(String idStr) {
        if (idStr == null || "".equals(idStr.trim())) {
            return false;
        } else if (idStr.matches("s*1[3578]\\d{9}")) {
            return true;
        }
        return false;
    }
    //密码格式判断
    public static boolean isPsw(String idStr) {
        Log.d(TAG, "isPsw: "+idStr.toString());
        if (idStr == null || "".equals(idStr.trim())) {
            return false;
        } else if (idStr.matches("[0-9a-zA-Z]{3,10}")) {
            return true;
        }
        return false;
    }
}
